package cn.xz.kjg.net;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;

/**
 * Created by yangkun on 2018/4/1.
 */

public class NetClient extends AsyncTask<byte[], Void, byte[]> {

    private Socket _sockClient;
    private String _strIp;
    private int _nPort;
    private OutputStream _os;


    public int initSocket(String Ip, int nPort) throws IOException {
        Log.e("ipport:",Ip+":"+nPort);
        _strIp = Ip;
        _nPort = nPort;
        return 1;
    }

    /**
     * @param strSnd the str to send
     * @return the length of send str
     * @throws IOException
     */
    public byte[] sendData(byte[] strSnd) throws IOException, NullPointerException {
        _os = _sockClient.getOutputStream();
        Log.e("sendOpen:", Arrays.toString(strSnd));
        _os.write(strSnd);
        return strSnd;
    }

    protected byte[] doInBackground(byte[]... params) {
        try {
            _sockClient = new Socket(_strIp, _nPort);
            _sockClient.setSoTimeout(5000);
            sendData(params[0]);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
